// const mysql = require('mysql2/promise');

// async function main() {
//   const connection = await mysql.createConnection({
//     host: 'localhost',
//     user: 'test',
//     password: 'test',
//     database: 'blog',
//     waitForConnections: true,
//     connectionLimit: 10,
//     queueLimit: 0
//   });

//   const [rows, fields] = await connection.execute('SELECT * FROM users where username =?');
//   console.log(rows); // 输出查询结果
//   console.log(fields); // 输出字段信息

//   await connection.end();
// }

// main().catch(console.error);


const mysql = require('mysql2/promise');

const pool = mysql.createPool({
    host: 'localhost',
    user: 'test',
    password: 'test',
    database: 'blog',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
});

// 一个用于执行查询的函数
async function query(sql, params) {
    const connection = await pool.getConnection();
    try {
        const [rows, fields] = await connection.execute(sql, params);
        return rows;
    } catch (error) {
        throw error;
    } finally {
        connection.release();
    }
}

// 示例使用
async function fetchData() {
    try {
        const result = await query('SELECT * FROM users where username =?', ['test']);
        console.log(result);
        return result;
    } catch (err) {
        console.error(err);
    }
}

fetchData().then(data => { console.log(data) });



// const { getUserByEmail, createUser } = require('./user');
// getUserByEmail('test@test.com')

